<?php

/* 视频管理 */
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\facade\Db;

class VideoController extends AdminBaseController
{
    var $type_private=[
        '0'=>'否',
        '1'=>'是'
    ];
    
    var $status=[
        '0'=>'待审核',
        '1'=>'审核通过',
        '2'=>'审核拒绝',
        '3'=>'下架'
    ];

    public function index()
    {
        
        $data = $this->request->param();
        $map=[];
		$status=isset($data['status']) ? $data['status']: '';
        if($status!=''){
            $map[]=['status','=',$status];
        }

        $uid=isset($data['uid']) ? $data['uid']: '';
        if($uid!=''){
            $map[]=['uid','=',$uid];
        }

        $list = Db::name('video')
            ->where($map)
            ->order("id desc")
            ->paginate(20);
        $list->each(function($v, $k){
            $userinfo=getUserInfo($v['uid']);
            $v['userinfo']=$userinfo;
            $v['thumb']=get_upload_path($v['thumb']);
            $v['href']=get_upload_path($v['href']);
            return $v;
        });
        
        $page = $list->render();
        $this->assign("page", $page);
            
        $this->assign('list', $list);
        $this->assign('type_private', $this->type_private);
        $this->assign('status', $this->status);


        return $this->fetch();
    }

    public function view()
    {
        $id   = $this->request->param('id', 0, 'intval');
        
        $data=Db::name('video')
            ->where("id={$id}")
            ->find();
        if(!$data){
            $this->error("信息错误");
        }
        
        $data['href']=get_upload_path($data['href']);
        
        $this->assign('data', $data);
        return $this->fetch();
    }
    
    public function add(){
        return $this->fetch();
    }

    public function addPost()
    {
        if ($this->request->isPost()) {
            $data      = $this->request->param();
            
            $uid=$data['uid'];

            if($uid < 0){
                $this->error('请填写正确的用户ID');
            }else{
                $check = Db::name('user')->where("id='{$uid}'")->find();
                if(!$check){
                    $this->error('该用户不存在');
                }
            }
            
            
            $isprivate=$data['isprivate'];
            $thumb=$data['thumb'];
            $coin=$data['coin'];
            
            if($isprivate==1){
                if($coin<=0){
                    $this->error('请输入正确价格');
                }
                
            }

            if($thumb==''){
                $this->error('请上传图片');
            }
                
            $data['addtime']=time();
            $data['uptime']=time();
            $data['status']='1';
            
            $id = Db::name('video')->insertGetId($data);
            if(!$id){
                $this->error("添加失败！");
            }
            $this->success("添加成功！");
        }
    }

    public function edit()
    {
        $id   = $this->request->param('id', 0, 'intval');
        
        $data=Db::name('video')
            ->where("id={$id}")
            ->find();
        if(!$data){
            $this->error("信息错误");
        }
        
        $data['userinfo']= getUserInfo($data['uid']);
        
        $this->assign('data', $data);
        return $this->fetch();
    }

    public function editPost()
    {
        if ($this->request->isPost()) {
            $data      = $this->request->param();

            $isprivate=$data['isprivate'];
            $thumb=$data['thumb'];
            $coin=$data['coin'];
            
            if($isprivate==1){
                if($coin<=0){
                    $this->error('请输入正确价格');
                }
            }

            if($thumb==''){
                $this->error('请上传图片');
            }
            
            $data['uptime']=time();
            
            $rs = Db::name('video')->update($data);

            if($rs === false){
                $this->error("保存失败！");
            }
            $this->success("保存成功！");
        }
    }


    public function del()
    {

        $data = $this->request->param();
        if(isset($data['id'])){
            $id = $data['id'];
            $rs = Db::name('video')->where("id={$id}")->delete();
            if(!$rs){
                $this->error("删除失败！");
            }

            Db::name("video_buy")->where("videoid={$id}")->delete();
            Db::name("video_comments")->where("videoid={$id}")->delete();
            Db::name("video_comments_like")->where("videoid={$id}")->delete();
            Db::name("video_like")->where("videoid={$id}")->delete();
            Db::name("video_view")->where("videoid={$id}")->delete();
        }

        if(isset($data['ids'])){
            $ids = $data['ids'];
            $rs = Db::name('video')->where('id', 'in', $ids)->delete();
            if(!$rs){
                $this->error("删除失败！");
            }

            Db::name("video_buy")->where('videoid', 'in', $ids)->delete();
            Db::name("video_comments")->where('videoid', 'in', $ids)->delete();
            Db::name("video_comments_like")->where('videoid', 'in', $ids)->delete();
            Db::name("video_like")->where('videoid', 'in', $ids)->delete();
            Db::name("video_view")->where('videoid', 'in', $ids)->delete();
        }
        
        
        
        $this->success("删除成功！",url("video/index"));
    }

    public function setstatus()
    {
        $id = $this->request->param('id', 0, 'intval');
        if(!$id){
            $this->error("数据传入失败！");
        }
        $status = $this->request->param('status', 0, 'intval');
        $reason = $this->request->param('reason');
        
        $result=Db::name("video")->where("id={$id}")->find();
        if(!$result){
            $this->error("数据传入失败！");
        }
        
        if($result['status']!=0 && $result['status'] == $status){
            $this->error("操作失败");
        }
        
        $nowtime=time();
        if($status==2){
            Db::name("video")->where("id={$id}")->delete();
        }else{
            $rs=Db::name("video")->where("id={$id}")->update(['status'=>$status,'reason'=>$reason,'uptime'=>$nowtime]);
            if(!$rs){
                $this->error("操作失败");
            }
        }
        
        
        /* IM */
        $content='您上传的视频审核未通过,已被删除，失败原因：'.$reason;
        $content_en='The video you uploaded did not pass the review and has been deleted. Reason for failure:'.$reason;
        if($status==1){
            $content='恭喜！您上传的视频审核通过了';
            $content_en='congratulations! The video you uploaded has been reviewed and approved';
        }
        sendIm($result['uid'],$content,$content_en);
        
        $this->success("操作成功");        
    }


    

}